<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\common\model;

use think\Model;

class ClassifyModel extends Model
{
    // 确定链接表名
    protected $name = 'classify';

    /**
     * 获取类别数据
     */
    public function getClassifyInfo($id)
    {
        $result = $this->where(['clas_delete'=>0])->field('clas_id,clas_name,clas_parentId')->select();
        $str = '';


        foreach($result as $key=>$vo){
            $str .= '{ "id": "' . $vo['clas_id'] . '", "pId":"' . $vo['clas_parentId'] . '", "name":"' . $vo['clas_name'].'"';

            if(!empty($rule) && in_array($vo['clas_id'], $rule)){
                $str .= ' ,"checked":1';
            }

            $str .= '},';

        }

        return '[' . rtrim($str, ',') . ']';
    }

    /**
     * 根据类别数据获取对应的菜单
     * @param $nodeStr
     */
    public function getMenu($nodeStr = '')
    {
        if(empty($nodeStr)){
            return [];
        }
        // 超级管理员没有类别数组 * 号表示
        $where = '*' == $nodeStr ? 'is_menu = 2' : 'is_menu = 2 and id in(' . $nodeStr . ')';

        $result = $this->field('id,node_name,type_id,control_name,action_name,style')
            ->where($where)->select();
        $menu = prepareMenu($result);

        return $menu;
    }

    /**
     * 根据条件获取访问权限类别数据
     * @param $where
     */
    public function getActions($where)
    {
        return $this->field('control_name,action_name')->where($where)->select();
    }

    /**
     * 获取类别数据
     * @return mixed
     */
    public function getClassifyList()
    {
        return $this->where(['clas_delete'=>0])->field('clas_id id,clas_name name,clas_parentId pid')->select();
    }

    /**
     * 插入类别信息
     * @param $param
     */
    public function insertClassify($param)
    {
        try{

            $this->save($param);
            return msg(1, '', '添加类别成功');
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 编辑类别
     * @param $param
     */
    public function editClassify($param)
    {
        try{
            $this->save($param, ['clas_id' => $param['clas_id']]);
            return msg(1, '', '编辑类别成功');
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 删除类别
     * @param $id
     */
    public function delClassify($id)
    {

        try{

            $this->save(['clas_delete'=>1], ['clas_id' => $id]);
            return msg(1, '', '删除类别成功');
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }

    //获取子分类
    public function getClassifySon($where){
        return $this->where($where)->select();

    }

    //获取全部分类
    public function getClassifyAll($where){
        return $this->where($where)->field('clas_id,clas_name')->select();
    }

    //获取一个分类
    public function getClassifyOne($id){
        return $this->where(['clas_delete'=>0,'clas_id'=>$id])->field('clas_id,clas_name')->find();
    }
}
